#!/bin/bash -e
# shellcheck source=meta-facebook/meta-yosemite4/recipes-yosemite4/plat-tool/files/yosemite4-common-functions
source /usr/libexec/yosemite4-common-functions

# wait for entity manager
mapper wait /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board
mapper wait /xyz/openbmc_project/inventory/system/board/Yosemite_4_Medusa_Board

# probe devices behind mux for management board cpld
mgm_stage=$(get_product_version Management_Board)

if [ -z "$mgm_stage" ]; then
    echo "Failed to check management board fru info, all CPLD I/O expender are keeping default setting"
elif [ "$mgm_stage" = "DVT" ] || [ "$mgm_stage" = "EVT" ]; then
    echo pca9506 "0x20" > /sys/bus/i2c/devices/i2c-13/new_device
    echo pca9506 "0x21" > /sys/bus/i2c/devices/i2c-13/new_device
    echo pca9506 "0x22" > /sys/bus/i2c/devices/i2c-13/new_device
    echo pca9506 "0x23" > /sys/bus/i2c/devices/i2c-13/new_device
fi

# set initial value for pca9555 i/o pins on medusa board
medusa_stage=$(get_product_version Medusa_Board)

if [ -z "$medusa_stage" ]; then
    echo "Failed to check medusa board fru info, all I/O pins are keeping default input"
elif [ "$medusa_stage" = "POC" ]; then
    set_gpio P48V_OCP_GPIO1       0
    set_gpio P48V_OCP_GPIO2       0
    set_gpio P48V_OCP_GPIO3       0
    set_gpio RST_MUX_R_N          1
    set_gpio RST_LED_CONTROL_FAN_BOARD_0_N 1
    set_gpio RST_LED_CONTROL_FAN_BOARD_1_N 1
    set_gpio RST_IOEXP_FAN_BOARD_0_N       1
    set_gpio RST_IOEXP_FAN_BOARD_1_N       1
    set_gpio HSC_OCP_SLOT_ODD_GPIO3        1
    set_gpio HSC_OCP_SLOT_EVEN_GPIO1       1
    set_gpio MEDUSA_BOARD_REV_0   0
    set_gpio MEDUSA_BOARD_REV_1   0
    set_gpio MEDUSA_BOARD_REV_2   0
    set_gpio MEDUSA_BOARD_TYPE    0
    set_gpio DELTA_MODULE_TYPE    0
    set_gpio P12V_HSC_TYPE        0
else
    set_gpio P48V_OCP_GPIO1       0
    set_gpio P48V_OCP_GPIO2       0
    set_gpio P48V_OCP_GPIO3       0
    set_gpio RST_MUX_R_N          1
    set_gpio RST_LED_CONTROL_FAN_BOARD_0_N 1
    set_gpio RST_LED_CONTROL_FAN_BOARD_1_N 1
    set_gpio RST_IOEXP_FAN_BOARD_0_N       1
    set_gpio RST_IOEXP_FAN_BOARD_1_N       1
    set_gpio HSC_OCP_SLOT_ODD_GPIO1        0
    set_gpio HSC_OCP_SLOT_ODD_GPIO2        0
    set_gpio HSC_OCP_SLOT_ODD_GPIO3        0
    set_gpio HSC_OCP_SLOT_EVEN_GPIO1       0
    set_gpio HSC_OCP_SLOT_EVEN_GPIO2       0
    set_gpio HSC_OCP_SLOT_EVEN_GPIO3       0
fi

# check if Fan_Board_0 exists
if timeout 120 mapper wait /xyz/openbmc_project/inventory/system/board/Yosemite_4_Fan_Board_0; then
    fsc_source=$(busctl get-property xyz.openbmc_project.EntityManager /xyz/openbmc_project/inventory/system/board/Yosemite_4_Fan_Board_0 xyz.openbmc_project.Inventory.Decorator.Asset Model | awk '{print $4}')
else
    fsc_source="Unknown"
    echo "Fan_Board_0 not found, skipping..."
fi
# enable watchdog timeout for fan board 0

if [ "$fsc_source" = "FSC-MAX" ]; then
    i2cset -f -y 30 0x20 0x0 0x26
    i2cset -f -y 30 0x2f 0x0 0x26
elif [ "$fsc_source" = "FSC-NCT" ]; then
    i2cset -f -y 30 0x21 0x2A 0x8C
    i2cset -f -y 30 0x23 0x2A 0x8C
else
    echo "Failed to check fan board 0 fru info, watchdog timeout is keeping default setting"
fi

# check if Fan_Board_1 exists
if timeout 120 mapper wait /xyz/openbmc_project/inventory/system/board/Yosemite_4_Fan_Board_1; then
    fsc_source=$(busctl get-property xyz.openbmc_project.EntityManager /xyz/openbmc_project/inventory/system/board/Yosemite_4_Fan_Board_1 xyz.openbmc_project.Inventory.Decorator.Asset Model | awk '{print $4}')
else
    fsc_source="Unknown"
    echo "Fan_Board_1 not found, skipping..."
fi

# enable watchdog timeout for fan board 1

if [ "$fsc_source" = "FSC-MAX" ]; then
    i2cset -f -y 31 0x20 0x0 0x26
    i2cset -f -y 31 0x2f 0x0 0x26
elif [ "$fsc_source" = "FSC-NCT" ]; then
    i2cset -f -y 31 0x21 0x2A 0x8C
    i2cset -f -y 31 0x23 0x2A 0x8C
else
    echo "Failed to check fan board 1 fru info, watchdog timeout is keeping default setting"
fi
exit 0
